package ops.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import ops.controller.ControllerManager;
import ops.controller.SubscriptionController;
import ops.i18n.Resources;
import ops.util.ViewHelper;
import ops.view.forms.Form;
import ops.view.forms.component.ComboboxInput;
import ops.view.tables.Column;
import ops.view.tables.TableMaker;

public class SubscriptionPanel extends JPanel implements Refreshable {

	private SubscriptionController subscriptionController = ControllerManager.getInstance().getSubscriptionController();
	
	private JButton removeSubscriptionButton;
	private JButton addSubscriptionButton;
	private JButton switchTermsButton;
	
	private JPanel content;
	
	private TableMaker availableClassesTable;
	private TableMaker currentSubscriptionsTable;
	
	private Form form;
	private ComboboxInput termsList;

	public SubscriptionPanel() {
		super(new BorderLayout());
		
		content = new JPanel(new BorderLayout());
		add(content, BorderLayout.CENTER);
		
		init();
	}

	private void init() {
		initForm();
		
		initAvailableClasses();
		initCurrentSubscriptions();
	}
	
	private void initForm() {
		Resources resources = Resources.getInstance();
		
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		termsList = new ComboboxInput(resources.getString("term")+":", true, ControllerManager.getInstance().getTermController().getTermsList());		
		form = new Form();
		form.addInput("term", termsList);		
		panel.add(form.getPanel());
		
		switchTermsButton = new JButton("Switch"); // TODO i18n
		switchTermsButton.addActionListener(new ButtonListener());
		panel.add(switchTermsButton);
		
		content.add(panel, BorderLayout.NORTH);
	}

	private void initAvailableClasses() {		
		Resources resources = Resources.getInstance();

		JPanel panel = new JPanel(new BorderLayout());
		
		addSubscriptionButton = ViewHelper.makeAddButton();
		addSubscriptionButton.addActionListener(new ButtonListener());
		panel.add(addSubscriptionButton, BorderLayout.SOUTH);
		
		availableClassesTable = new TableMaker();
		availableClassesTable.addColumn(new Column(resources.getString("class.code"), Integer.class, 15));
		availableClassesTable.addColumn(new Column(resources.getString("class.subject"), String.class, 70));
		availableClassesTable.addColumn(new Column(resources.getString("class.size"), Integer.class, 3));
		availableClassesTable.addColumn(new Column(resources.getString("class.teacher"), String.class));
		availableClassesTable.setControlButtons(addSubscriptionButton);

		JScrollPane availableScroll = ViewHelper.makeScrollPane(availableClassesTable.getTable());
		availableScroll.setBorder(BorderFactory.createTitledBorder("Available"));
		availableScroll.setPreferredSize(new Dimension(2000,200));
		panel.add(availableScroll, BorderLayout.CENTER);
		
		content.add(panel, BorderLayout.CENTER);
	}

	private void initCurrentSubscriptions() {
		Resources resources = Resources.getInstance();
		
		JPanel panel = new JPanel(new BorderLayout());
		
		removeSubscriptionButton = ViewHelper.makeRemoveButton();
		removeSubscriptionButton.addActionListener(new ButtonListener());
		panel.add(removeSubscriptionButton, BorderLayout.SOUTH);
		
		currentSubscriptionsTable = new TableMaker();
		currentSubscriptionsTable.addColumn(new Column(resources.getString("subscription.code"), Integer.class));
		currentSubscriptionsTable.addColumn(new Column(resources.getString("subscription.class"), String.class));
		currentSubscriptionsTable.addColumn(new Column(resources.getString("subscription.date"), Date.class));
		currentSubscriptionsTable.setControlButtons(removeSubscriptionButton);
		
		JScrollPane currentScroll = ViewHelper.makeScrollPane(currentSubscriptionsTable.getTable());
		currentScroll.setBorder(BorderFactory.createTitledBorder("Current")); // TODO i18n
		currentScroll.setPreferredSize(new Dimension(2000, 200));
		panel.add(currentScroll, BorderLayout.CENTER);
		
		content.add(panel, BorderLayout.SOUTH);
	}
	
	public void addRowToCurrent(Object... data) {
		currentSubscriptionsTable.addRow(data);
	}
	
	public void addRowToAvailable(Object... data) {
		availableClassesTable.addRow(data);
	}

	@Override
	public void refresh() {
		subscriptionController.updateTables();
		termsList.updateData(ControllerManager.getInstance().getTermController().getTermsList());
	}

	public void clearTables() {
		availableClassesTable.clearTable();
		currentSubscriptionsTable.clearTable();
	}

	private class ButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			JButton button = (JButton) e.getSource();
			
			if (!button.isEnabled()) {
				return;
			}
			
			if (button == addSubscriptionButton) {
				Integer classCode = (Integer) availableClassesTable.getValueAt(availableClassesTable.getSeletedRow(), 0);
				subscriptionController.addSubscription(classCode);
			} else if (button == removeSubscriptionButton) {
				Integer subCode = (Integer) currentSubscriptionsTable.getValueAt(currentSubscriptionsTable.getSeletedRow(), 0);
				subscriptionController.removeSubscription(subCode);
			} else if (button == switchTermsButton) {
				Integer termCode = form.getIntegerFor("term");
				subscriptionController.setTermCode(termCode);
				subscriptionController.updateTables();
			}
		}
	}

}